﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.Data.Json;

namespace XiaoweiVoiceAI.ResponseCreators
{
    internal static class IOTResponseCreator
    {
        private class IOTResponseData : IIOTResponseData
        {
            public IReadOnlyDictionary<string, IIOTGroup> IOTGroups { get; set; }
            public IReadOnlyList<IIOTDevice> IOTDevices { get; set; }
            public ResponseType ResponseType => ResponseType.IOT;
            public ErrorCode ErrorCode => ErrorCode.NoError;
            public string RequestText { get; set; }
            public string ResponseText { get; set; }
            public string TTSUrl { get; set; }
            public bool RestoreInHistory { get; set; }
            public object DataContext { get; set; }
        }
        private class IOTDevice:IIOTDevice
        {
            public string DeviceIcon{ get; set; }
            public string DeviceRemarkName{get;set;}
            public IOTType Type { get; set; }
            public string GroupID { get; set; }
        }
        private class IOTGroup : IIOTGroup
        {
            public string GroupID { get; set; }
            public string GroupName{get;set;}
        }

        private static Dictionary<string, IOTType> iotTypeMap = new Dictionary<string, IOTType>()
        {
            {"switch",IOTType.Switch },
            {"light",IOTType.Light },
            {"aircondition",IOTType.Aircondition  },
            {"curtain",IOTType.Curtain  },
            {"outlet",IOTType.Outlet },
            {"television",IOTType.Television },
            {"fan",IOTType.Fan },
            {"airpurifier",IOTType.Airpurifier },
            {"ircontroller",IOTType.Ircontroller },
            {"bridge",IOTType.Bridge },
            {"ventilation",IOTType.Ventilation  },
            {"sweepingrobot",IOTType.Sweepingrobot  },
            {"humidifier",IOTType.Humidifier  },
            {"fridge",IOTType.Fridge  },
            {"robot",IOTType.Robot  },
            {"washmachine",IOTType.Washmachine  },
            {"waterdispenser",IOTType.Waterdispenser  },
            {"waterheater",IOTType.Waterheater  },
            {"cooker",IOTType.Cooker  },
            {"stb",IOTType.Stb  },
            {"projector",IOTType.Projector  },
            {"winecabinet",IOTType.Winecabinet },
            {"door",IOTType.Door  },
            {"dehumidifier",IOTType.Dehumidifier  },
            {"remoteccontrol",IOTType.Remoteccontrol  },
            {"camera",IOTType.Camera  },
            {"scene",IOTType.Scene }
        };

        public static IResponseData CreateResponseData(XiaoweiVoiceAI xiaoweiVoiceAI, XiaoweiV2.ResponseParam param)
        {
            if (String.IsNullOrEmpty(param.ResponseData)) return null;
            JsonObject jsonObject = JsonObject.Parse(param.ResponseData);
            Dictionary<string, IIOTGroup> iotGroups = new Dictionary<string, IIOTGroup>();
            List<IIOTDevice> iotDevices = null;
            jsonObject = jsonObject["data"].GetObject();
            foreach (var item in jsonObject["category_list"].GetObject())
            {
                IOTGroup iOTGroup = new IOTGroup()
                {
                    GroupID = item.Key,
                    GroupName = item.Value.GetString()
                };
                iotGroups.Add(iOTGroup.GroupID, iOTGroup);

            }
            if (jsonObject.ContainsKey("device_list"))
            {
                iotDevices = new List<IIOTDevice>();
                foreach (var item in jsonObject["device_list"].GetArray())
                {
                    var deviceobj = item.GetObject();
                    IOTDevice iOTDevice = new IOTDevice();
                    iOTDevice.DeviceIcon = deviceobj["device_icon"].GetString();
                    iOTDevice.DeviceRemarkName = deviceobj["device_name"].GetString();
                    string iotType = deviceobj["device_type"].GetString();
                    if (iotTypeMap.ContainsKey(iotType))
                    {
                        iOTDevice.Type = iotTypeMap[iotType];
                    }
                    else
                    {
                        iOTDevice.Type = IOTType.Unknow;
                    }
                    iOTDevice.GroupID = deviceobj["category_id"].GetNumber().ToString();
                    iotDevices.Add(iOTDevice);
                }
            }

            IOTResponseData iotResponseData = new IOTResponseData();
            iotResponseData.RequestText = param.RequestText;
            iotResponseData.ResponseText = param.ResponseText;
            iotResponseData.TTSUrl = XiaoweiV2.ResponseHelper.GetTTSUrl(param);
            iotResponseData.IOTGroups = iotGroups;
            iotResponseData.IOTDevices = iotDevices;
            iotResponseData.RestoreInHistory = !string.IsNullOrEmpty(param.RequestText);
            return iotResponseData;
        }
    }
}
